package com.realdtech.wechat.task.Controller;


import com.jfinal.kit.Ret;
import com.jfinal.upload.UploadFile;
import com.realdtech.base.jfinal.plugin.oss.OSS;
import com.realdtech.base.jfinal.plugin.oss.OSSKit;
import com.realdtech.base.ui.easyui.EasyUIController;
import com.realdtech.wechat.task.Service.*;
import com.realdtech.wechat.task.model.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2017/9/18.
 */
public class AdminController extends EasyUIController {


    public final UserService US=enhance(UserService.class);
    public final UserJoinService join=enhance(UserJoinService.class);
    public final UserCompanyService UC=enhance(UserCompanyService.class);
    public final UserApplyService UA=enhance(UserApplyService.class);
    public final DitchService DS=enhance(DitchService.class);//热门渠道
    public final NewsService NS=enhance(NewsService.class);
    public final ChannelInService channelInS=enhance(ChannelInService.class);//渠道入驻

    public void index(){

        render("login.html");
    }
    //登录跳转
    public void register(){

        String username=getPara("username");//登陆名
        String password=getPara("password");//密码

        int size=US.findAD(username,password).size();//查询数据库是否有这个账号或密码
        //登陆验证
        if(size>0){
            render("index.html");
        }else{
            render("login.html");
        }
    }

    //加载中间文字
    public void home(){

        render("home.html");
    }

    //加载左边消息栏
    public void menu(){

        render("menu.html");
    }

    /**
     * 渠道入驻
     */
    public void channel(){

        render("channel.html");
    }
    /**
     * 查询渠道入驻
     */
    public void channelIn(){

        List<ChannelIn> list=channelInS.findChannelIn();

        String s="{\"total\":"+list.size()+",\"rows\":[]}";
        System.out.println("看看"+s);

        JSONArray array = new JSONArray();

        for(ChannelIn tkt:list){

            String jsonStr="{\"name\":"+tkt.getName()+",\"phone\":"+tkt.getPhone()+",\"company\":"+tkt.getCompany()+",\"address\":"+tkt.getAddress()+",\"product\":"+tkt.getProduct()+",\"date\":\"" + tkt.getDate() + "\"}";

            JSONObject jsonObj = new JSONObject(jsonStr);
            array.put(jsonObj);
        }
        JSONObject json= JSONObject.fromObject(s);
        json.put("rows",array.toString());

        renderJson(json.toString());
    }

    /**
     * 查询加盟申请
     */
    public void alliance(){

        render("table.html");
    }

    //查询加盟申请数据
    public void toTable(){

        List<UserJoin> list=join.findJoin();

        String s="{\"total\":"+list.size()+",\"rows\":[]}";

        JSONArray array = new JSONArray();
        for(UserJoin tkt:list){

            String jsonStr="{\"productid\":\""+tkt.getPhone()+"\",\"unitcost\":\""+tkt.getDate()+"\",\"invitenum\":"+tkt.getRegion()+",\"attr1\":\""+tkt.getRemark()+"\",\"itemid\":\""+tkt.getName()+"\"}";

            JSONObject jsonObj = new JSONObject(jsonStr);
            array.put(jsonObj);
        }
        JSONObject json= JSONObject.fromObject(s);
        json.put("rows",array.toString());

        renderJson(json.toString());
    }

    /**
     * 企业信息
     */
    public void company(){

        render("company.html");
    }

    //查询企业信息
    public void find_company(){

        List<UserCompany> list=UC.find();
        String s="{\"total\":"+list.size()+",\"rows\":[]}";
        JSONArray array = new JSONArray();
        for(UserCompany tkt:list){
            System.out.println(tkt);
            String jsonStr="{\"company\":\""+tkt.getCompany()+"\",\"productname\":\"Amazon Parrot\",\"name\":\""+tkt.getName()+"\",\"status\":\"P\",\"email\":"+tkt.getEmail()+",\"phone\":\""+tkt.getPhone()+"\",\"date\":\""+tkt.getDate()+"\"}";
            JSONObject jsonObj = new JSONObject(jsonStr);
            array.put(jsonObj);
        }
        JSONObject json= JSONObject.fromObject(s);
        json.put("rows",array.toString());
        renderJson(json.toString());
    }

    /**
     * 快速申请
     */
    //跳转
    public void apply(){

        render("apply.html");
    }

    //查询快速申请数据
    public void apply_url(){

        List<UserApply> list=UA.find();
        String s="{\"total\":"+list.size()+",\"rows\":[]}";

        JSONArray array = new JSONArray();
        for(UserApply tkt:list){
            System.out.println(tkt);
            String jsonStr="{\"name\":\""+tkt.getUserName()+"\",\"productname\":\"Amazon Parrot\",\"phone\":\""+tkt.getPhone()+"\",\"status\":\"P\",\"money\":\""+tkt.getMoney()+"\",\"date\":\""+tkt.getDate()+"\",\"image_name\":\""+tkt.getImageName()+"\",\"limit\":\"" + tkt.getLimit() + "\",\"interest\":\""+tkt.getInterest()+"\",\"deadline\":\""+tkt.getDeadline()+"\",\"repayment\":\""+tkt.getRepayment()+"\",\"product\":\""+tkt.getProduct()+"\"}";
            JSONObject jsonObj = new JSONObject(jsonStr);
            array.put(jsonObj);
        }

        JSONObject json= JSONObject.fromObject(s);
        json.put("rows",array.toString());
        System.out.println(array.toString());
        System.out.println(json.toString());
        renderJson(json.toString());
    }

    /**
     * 热门渠道 内容管理
     */
    //热门渠道综合管理
    public void credit(){

        String id=getPara("id");
        int ID = Integer.valueOf(id).intValue();

        //根据ID来判断是什么页面 1=信用贷款 2=车辆贷款 3=房屋贷款 4=企业贷款
        if(ID==1){
            List<DitchCredit> list=DS.find();
            setAttr("size",list.size());
            setAttr("list",list);
            setAttr("id","1");
        }else if(ID==2){
            List<DitchVehicle>list=DS.findV();
            setAttr("size",list.size());
            setAttr("list",list);
            setAttr("id","2");
        }else if(ID==3){
            List<DitchMortgage>list=DS.findM();
            setAttr("size",list.size());
            setAttr("list",list);
            setAttr("id","3");
        }else if(ID==4){
            List<DitchEnterprise>list=DS.findE();
            setAttr("size",list.size());
            setAttr("list",list);
            setAttr("id","4");
        }
        setAttr("a",1);
        render("particulars.html");
    }

    //跳转
    public void add(){

        String ID=getPara("id");
        //1=信用贷款 2=车辆贷款 3=房屋贷款 4=企业贷款
        //100=轮播图 101=金融咨询 102=行业资讯 103=贷款攻略 104=公告栏 105=热点资讯 106=合作商风采
        if(ID.equals("1")){
            setAttr("id",1);
        }else if(ID.equals("2")){
            setAttr("id",2);
        }else if(ID.equals("3")){
            setAttr("id",3);
        }else if(ID.equals("4")){
            setAttr("id",4);
        }else if(ID.equals("100")){
            setAttr("id",100);
        }else if(ID.equals("101")){
            setAttr("id",101);
        }else if(ID.equals("102")){
            setAttr("id",102);
        }else if(ID.equals("103")){
            setAttr("id",103);
        }else if(ID.equals("104")){
            setAttr("id",104);
        }else if(ID.equals("105")){
            setAttr("off_on","off");
            setAttr("id",105);
        }else if(ID.equals("106")){
            setAttr("id",106);
        }
            render("detail.html");
    }

    //提交 新增 热门渠道||内容管理
    public void submit_detail() {

        String id=getPara("id");
        //往oss上上传图片
        String image=" ";
        OSS oss = OSSKit.use("reald-upload");
        //上传的图标文件
        UploadFile iconFile = getFile();
        DitchCredit bizChannel = getModel(DitchCredit.class);
        if (iconFile != null) {
            File f = iconFile.getFile();
            String key = oss.generateKey("finchain-zhaoshang", f);//设置oss中间保存路径
            oss.getClient().putObject(oss.getBucketName(), key, f);
            image=oss.getLastUrl()+"/"+key;//获取头像路径
            f.delete();
        }

        String ID=getPara("ID");//渠道名称  -- 大标题  -- 外部链接
        String limit=getPara("limit");//贷款额度 -- 作者
        String nickname=getPara("nickname");//贷款利率 -- 来源
        String name=getPara("name");//批款速度
        String tel=getPara("tel");//贷款期限
        String createTime=getPara("createTime");//还款方式
        String state=getPara("state");//产品类型
        String content=getPara("content");//申请条件   -- 文章内容
        String title=getPara("title");//图片描述

        if(id.equals("1")){//信用贷款
            DS.add_financial(image,ID,limit,nickname,name,tel,createTime,state,content);
        }else if(id.equals("2")){//车辆贷款
            DS.add_V(image,ID,limit,nickname,name,tel,createTime,state,content);
        }else if(id.equals("3")){//房屋贷款
            DS.add_M(image,ID,limit,nickname,name,tel,createTime,state,content);
        }else if(id.equals("4")){
            DS.add_E(image,ID,limit,nickname,name,tel,createTime,state,content);
        }else if(id.equals("100")){
            NS.add_Slideshow(image,ID);
        }else if(id.equals("101")){
            NS.add_F(ID,image,content,limit,nickname);
        }else if(id.equals("102")){
            NS.add_B(ID,image,content,limit,nickname);
        }else if(id.equals("103")){
            NS.add_S(ID,image,content,limit,nickname);
        }else if(id.equals("104")){
            NS.add_BU(ID,image,content,limit,nickname);
        }else if(id.equals("105")){
            NS.add_H(ID,content,limit,nickname);
        }else if (id.equals("106")){
            NS.add_Partnership(image,title,ID);
        }
        renderJson(Ret.create("success", true));
    }

    //热门渠道编辑
    public void compile(){

        String U_id=getPara("u_id");//数据库编号
        String id=getPara("id");//id
        if(U_id.equals("1")){//信用贷款
            System.out.println("信用贷款,U_id:"+U_id);
            DitchCredit list=DS.findDs(id);
            setAttr("id",U_id);
            setAttr("li",list);
        }else if(U_id.equals("2")){
            System.out.println("车辆贷款");
            DitchVehicle list =DS.findVs(id);
            setAttr("id",U_id);
            setAttr("li",list);
        }else if(U_id.equals("3")){
            System.out.println("房屋贷款");
            DitchMortgage list =DS.findMs(id);
            setAttr("id",U_id);
            setAttr("li",list);
        }else if(U_id.equals("4")){
            System.out.println("企业贷款");
            DitchEnterprise list =DS.findEs(id);
            setAttr("id",U_id);
            setAttr("li",list);
        }
        render("compile.html");
//        renderJson(Ret.create("success", true));

    }

    //内容管理编辑
    public void content(){

        String U_id=getPara("u_id");//数据库编号
        String id=getPara("id");//id

        if(U_id.equals("100")) {//轮播图
            System.out.println("轮播图");
            NewsSlideshow list=NS.find_Slideshow_s(id);
            setAttr("id",U_id);
            setAttr("li",list);
        }else if(U_id.equals("101")){//金融咨询
            System.out.println("金融咨询,U_id:"+U_id);
            NewsFinance list=NS.findFids(id);
            setAttr("id",U_id);
            setAttr("li",list);
        }else if(U_id.equals("102")){
            System.out.println("行业资讯");
            NewsBusiness list =NS.findBids(id);
            setAttr("id",U_id);
            setAttr("li",list);
        }else if(U_id.equals("103")){
            System.out.println("贷款攻略");
            NewsStrategy list =NS.findSids(id);
            setAttr("id",U_id);
            setAttr("li",list);
        }else if(U_id.equals("104")){
            System.out.println("公告栏");
            NewsBulletin list =NS.findBUids(id);
            setAttr("id",U_id);
            setAttr("li",list);
        }else if(U_id.equals("105")){

            System.out.println("热点资讯");
            NewsHot list =NS.findHids(id);
            setAttr("id",U_id);
            setAttr("li",list);
            setAttr("on_off","off");
        }else if (U_id.equals("106")){

            System.out.println("合作商风采");
            NewsPartnership list =NS.find_Partnership_s(id);
            setAttr("id",U_id);
            setAttr("li",list);
        }
        render("compile.html");
    }

    //更新热门渠道||内容管理
    public void update(){

        //往oss上上传图片
        String img=" ";
        OSS oss = OSSKit.use("reald-upload");

        //上传的图标文件
        UploadFile iconFile = getFile();
        DitchCredit bizChannel = getModel(DitchCredit.class);
        if (iconFile != null) {
            File f = iconFile.getFile();
            String key = oss.generateKey("finchain-zhaoshang", f);//设置oss中间保存路径
            oss.getClient().putObject(oss.getBucketName(), key, f);
            img=oss.getLastUrl()+"/"+key;//获取头像路径
            f.delete();
        }

        String U_id=getPara("u_id");//标识
        String id=getPara("id");//数据库内容的id
        String image=getPara("is");//隐藏图名称
        String ID=getPara("ID");//渠道名称  -- 大标题
        String limit=getPara("limit");//贷款额度 -- 作者
        String nickname=getPara("nickname");//贷款利率 -- 来源
        String name=getPara("name");//批款速度
        String tel=getPara("tel");//贷款期限
        String createTime=getPara("createTime");//还款方式
        String state=getPara("state");//产品类型
        String content=getPara("content");//申请条件   -- 文章内容
        String title=getPara("title");//图片描述

        if(img!=" "){
            System.out.println("图不为空");
            image=img;//更换图片
        }
        if(U_id.equals("1")){//以下是热门渠道
            DS.updateC(image,ID,limit,nickname,name,tel,createTime,state,content,id);
        }else if(U_id.equals("2")){
            DS.updateV(image,ID,limit,nickname,name,tel,createTime,state,content,id);
        }else if(U_id.equals("3")){
            DS.updateM(image,ID,limit,nickname,name,tel,createTime,state,content,id);
        }else if(U_id.equals("4")){
            DS.updateE(image,ID,limit,nickname,name,tel,createTime,state,content,id);
        }else if(U_id.equals("100")){//以下是内容管理
            NS.update_Slideshow(image,ID,id);
        }else if(U_id.equals("101")){
            NS.updateF(ID,image,content,limit,nickname,id);
        }else if(U_id.equals("102")){
            NS.updateB(ID,image,content,limit,nickname,id);
        }else if(U_id.equals("103")){
            NS.updateS(ID,image,content,limit,nickname,id);
        }else if(U_id.equals("104")){
            NS.updateBU(ID,image,content,limit,nickname,id);
        }else if(U_id.equals("105")){
            NS.updateH(ID,content,limit,nickname,id);
        }else if(U_id.equals("106")){
            NS.update_Partnership(image,title,ID,id);
        }
        renderJson(Ret.create("success", true));
    }

    //查看
    public void  check(){

        String U_id=getPara("u_id");//数据库编号
        String id=getPara("id");//id

        if(U_id.equals("1")){//信用贷款
            System.out.println("信用贷款");
            DitchCredit list=DS.findDs(id);
            setAttr("li",list);
        }else if(U_id.equals("2")){
            System.out.println("车辆贷款");
            DitchVehicle list =DS.findVs(id);
            setAttr("li",list);
        }else if(U_id.equals("3")){
            System.out.println("房屋贷款");
            DitchMortgage list =DS.findMs(id);
            setAttr("li",list);
        }else if(U_id.equals("4")){
            System.out.println("企业贷款");
            DitchEnterprise list =DS.findEs(id);
            setAttr("li",list);
        }if(U_id.equals("100")) {//轮播图
            System.out.println("轮播图");
            NewsSlideshow list=NS.find_Slideshow_s(id);
            setAttr("li",list);
        }else if(U_id.equals("101")){//金融咨询
            System.out.println("金融咨询,U_id:"+U_id);
            NewsFinance list=NS.findFids(id);
            setAttr("li",list);
        }else if(U_id.equals("102")){
            System.out.println("行业资讯");
            NewsBusiness list =NS.findBids(id);
            setAttr("li",list);
        }else if(U_id.equals("103")){
            System.out.println("贷款攻略");
            NewsStrategy list =NS.findSids(id);
            setAttr("li",list);
        }else if(U_id.equals("104")){
            System.out.println("公告栏");
            NewsBulletin list =NS.findBUids(id);
            setAttr("li",list);
        }else if(U_id.equals("105")){
            System.out.println("热点资讯");
            NewsHot list =NS.findHids(id);
            setAttr("li",list);
            setAttr("on_off","off");
        }else if (U_id.equals("106")){
            System.out.println("合作商风采");
            NewsPartnership list =NS.find_Partnership_s(id);
            setAttr("li",list);
        }
        setAttr("id",U_id);
        render("ueditor_ck.html");
    }

    //删除
    public void delete(){

        String U_id=getPara("u_id");
        String id=getPara("id");

        if(U_id.equals("1")){//以下是热门渠道
            DS.delete_D(id);
        }else if(U_id.equals("2")){
            DS.delete_V(id);
        }else if(U_id.equals("3")){
            DS.delete_M(id);
        }else if(U_id.equals("4")){
            DS.delete_E(id);
        }else if(U_id.equals("100")){//以下是内容管理
            NS.delete_Slideshow(id);
        }else if(U_id.equals("101")){
            NS.delete_F(id);
        }else if(U_id.equals("102")){
            NS.delete_B(id);
        }else if(U_id.equals("103")){
            NS.delete_S(id);
        }else if(U_id.equals("104")){
            NS.delete_BU(id);
        }else if(U_id.equals("105")){
            NS.delete_H(id);
        }else if(U_id.equals("106")){
            NS.delete_Partnership(id);
        }
        renderText("删除成功");
    }

    /**
     * 首页内容管理
     */
    //优质业务
    public void high(){

        renderText("尽情期待");
    }

    //内容管理
    public void information(){

        String id=getPara("id");
        setAttr("a",2);

        if(id.equals("100")) {
            //轮播图
            List<NewsSlideshow>list=NS.find_Slideshow__desc();
            setAttr("a",3);
            setAttr("size",list.size());
            setAttr("list",list);
            setAttr("id","100");
            render("particulars.html");
            return;
        }else if(id.equals("101")){//金融咨询
            List<NewsFinance>list=NS.findF_desc();
            setAttr("a",7);
            setAttr("size",list.size());
            setAttr("list",list);
            setAttr("id","101");
            render("particulars.html");
            return;
        }else if(id.equals("102")){//行业资讯
            List<NewsBusiness>list=NS.findB_desc();
            setAttr("a",8);
            setAttr("size",list.size());
            setAttr("list",list);
            setAttr("id","102");
            render("particulars.html");
            return;
        }else if(id.equals("103")){//贷款攻略
            List<NewsStrategy>list=NS.findS_desc();
            setAttr("a",9);
            setAttr("size",list.size());
            setAttr("list",list);
            setAttr("id","103");
            render("particulars.html");
            return;
        }else if(id.equals("104")){//公告栏
            List<NewsBulletin>list=NS.findBU_desc();
            setAttr("a",6);
            setAttr("size",list.size());
            setAttr("list",list);
            setAttr("id","104");
            render("particulars.html");
            return;
        }else if(id.equals("105")){//热点资讯
            List<NewsHot>list=NS.findH_desc();
            setAttr("a",5);
            setAttr("size",list.size());
            setAttr("list",list);
            setAttr("id","105");
            render("particulars.html");
            return;
        }else if(id.equals("106")){//合作商风采
            List<NewsPartnership> list=NS.find_Partnership__desc();
            setAttr("a",4);
            setAttr("size",list.size());
            setAttr("list",list);
            setAttr("id","106");
            render("particulars.html");
            return;
        }
        renderText("尽情期待~");
    }
}